<template>
  <div class="search-result">
      <van-nav-bar
      :title="$route.query.keyword+'的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push('/article/'+item.art_id.toString())"
      >
          <template #label>
            <van-grid :column-num="item.cover.images.length" >
                <van-grid-item v-for="(item, index) in item.cover.images" :key="index" icon="photo-o" text="文字" >
                    <van-image lazy-load :src="item" style="height:80%" />
                </van-grid-item>
            </van-grid>
          </template>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { reqGetSearch } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  methods: {
    async onLoad () {
      // 异步更新数据
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1)
    //     }
    //     // 加载状态结束
    //     this.loading = false

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true
      //     }
      //   }, 500)
      const keyword = this.$route.query.keyword
      const res = await reqGetSearch(keyword, this.page)
      //   console.log(res.data.data)
      const arr = res.data.data.results
      this.list = [...this.list, ...arr]
      this.page++
      this.loading = false
      if (arr.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}

</style>
<style lang="scss">

</style>
